<template>
  <div>
    <all-header title="实名认证" />
    <template v-if="!userInfo.idcard">
      <div class="form">
        <div class="username">
          <input v-model="params.realname" placeholder="真实姓名" />
        </div>
        <div class="password">
          <input v-model="params.idcard" placeholder="身份证号" />
        </div>
      </div>
      <all-btn title="开始认证" :loading="loading" @click="toAuth" />
    </template>

    <template v-else>
      <div class="success">
        <img src="../../assets/image/my/success.png" alt="" />
        <div class="sc_text">实名认证成功</div>
      </div>
    </template>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import userStore from "../../store/user";
import commonStore from "../../store/common.js";
import { Toast } from "vant";

const userInfo = computed(() => commonStore().userInfo);

const params = ref({
  realname: "",
  idcard: "",
});

const isIdCard = (idCard) => {
  console.log(idCard);
  // 15位和18位身份证号码的正则表达式
  var regIdCard =
    /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
  if (regIdCard.test(idCard)) {
    if (idCard.length == 18) {
      var idCardWi = new Array(
        7,
        9,
        10,
        5,
        8,
        4,
        2,
        1,
        6,
        3,
        7,
        9,
        10,
        5,
        8,
        4,
        2
      ); // 将前17位加权因子保存在数组里
      var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); // 这是除以11后，可能产生的11位余数、验证码，也保存成数组
      var idCardWiSum = 0; // 用来保存前17位各自乖以加权因子后的总和
      for (var i = 0; i < 17; i++) {
        idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
      }

      var idCardMod = idCardWiSum % 11; // 计算出校验码所在数组的位置
      var idCardLast = idCard.substring(17); // 得到最后一位身份证号码

      // 如果等于2，则说明校验码是10，身份证号码最后一位应该是X
      if (idCardMod == 2) {
        if (idCardLast == "X" || idCardLast == "x") {
          return true;
        } else {
          return false;
        }
      } else {
        // 用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
        if (idCardLast == idCardY[idCardMod]) {
          return true;
        } else {
          return false;
        }
      }
    } else {
      return true;
    }
  } else {
    return false;
  }
};

const user = userStore();
const loading = ref(false);
const toAuth = () => {
  if (!params.value.realname) {
    return Toast.fail("请填写姓名");
  }
  if (!isIdCard(params.value.idcard)) {
    return Toast.fail("身份证号格式错误");
  }
  loading.value = true;
  user
    .toAuth(params.value)
    .then(() => {
      loading.value = false;
      Toast.success("提交成功");
    })
    .catch(() => {
      loading.value = false;
    });
};
</script>


<style lang="scss" scoped>
.form {
  padding: 0.3846rem;
  padding-top: 20%;

  input {
    font-size: 0.2885rem;
    width: 100%;
    margin-bottom: 0.7692rem;
    padding-bottom: 0.1923rem;
    border: none;
    border-bottom: 0.0192rem solid #e8e8e8;
  }
}
.success {
  padding-top: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  font-weight: bold;
  .sc_text {
    font-size: 15px;
  }
  img {
    width: 100px;
    height: 100px;
    padding-bottom: 20px;
  }
}
</style>